package ModelLayer;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

/**
 * Write a description of class FileStock here.
 * 
 * @author (Georgi Georgiev) 
 * @version (10.12.2013)
 */
public class FileStock
{
    private File file;
    private BufferedWriter bw;
    private FileWriter fw;
    private BufferedReader br;
    private FileReader fr;
    private ArrayList<Product> old_products;
    /**
     * Constructor for objects of class FileStock
     */
    public FileStock()
    {
       file = new File("Products.txt");
       if(!(file.exists()))
       {
           try
           {
               file.createNewFile();
           }
           
           catch (IOException e)
           {
               
           }
       }
       old_products = get_data();
          
    }

    /**
     * 
     */
    public boolean save_changes(ArrayList<Product> new_products)
    {
        if(has_changed(new_products))
        {            
            try
            {
                fw = new FileWriter(file);
                bw = new BufferedWriter(fw);
                for(Product articule : new_products)
                {
                    String line = articule.give_string_data();
                    bw.write(line);
                    bw.newLine();
                }
                bw.close();
                old_products = get_data();
                return true;
            }
            catch (IOException e)
            {
            }
        }
        return false;
    }
    
    /**
     * 
     */
    private boolean has_changed(ArrayList<Product> new_product)
    {
        int size;
        if((size = old_products.size()) == new_product.size())
        {
            for(int i = 0; i < size; i++)
            {
                if(!(old_products.get(i).give_string_data().equals(new_product.get(i).give_string_data())))
                {
                    return true;
                }
            }
            return false;
        }
        return true;
    }
    
    /**
     * 
     */
    public ArrayList<Product> get_data()
    {
        ArrayList<Product> product_from_file = new ArrayList<>();
        String line;
        try
        {
            fr = new FileReader(file);
            try
            {
                br = new BufferedReader(fr);
                while ((line = br.readLine()) != null)
                {
                    if(line != "")
                    {
                        product_from_file.add(retrive_product(line));
                    }
                }
                br.close();
            }
            catch (IOException e)
            {
            }
        }
        catch (FileNotFoundException ex)
        {
        }
        return product_from_file;
    }
    
    /**
     * 
     */
    private Product retrive_product(String line)
    {
        String[] data = line.split("\t");
        double price = Double.parseDouble(data[4]);
        double price_for_pallete = Double.parseDouble(data[5]);
        int pieces_in_pallete = Integer.parseInt(data[6]);
        int quantity = Integer.parseInt(data[7]);
        int maximum_amount = Integer.parseInt(data[8]);
        int minimum_amount = Integer.parseInt(data[9]);
        int amount_qualified_for_discount = Integer.parseInt(data[10]);
        double discount = Double.parseDouble(data[12]);
        return new Product(data[0], data[1], data[2], data[3], price, price_for_pallete, pieces_in_pallete, quantity, maximum_amount, minimum_amount, amount_qualified_for_discount, data[11], discount);
    }     
}
